<?php
/**
 * Description of DataBaseSource
 *
 * @author Diego
 */
class DataBaseSource implements IDropDownDataSource{
    
    /**
     * The custom query in case of CUSTOM_QUERY is set as source mode
     * @var string 
     */
    private $_sql;

    
    /**
     * The object responsible to fetch the options data from the DB.
     * @var SimpleSelect 
     */
    private $_simpleSelect;

    /**
     * The selected field which will become in the text attribute of the dropbox option
     * @var string 
     */
    private $_optionValueField;

    /**
     * The selected field which will become in the value attribute of the dropbox option
     * @var string 
     */
    private $_optionTextField;
    
    /**
     * 	Retrieve from the database the parsed data needed for the dropdown options 
     * @return array 
     */
    private function _getDataFromDatabase() {
        $this->_simpleSelect->execute();
        $results = $this->_simpleSelect->getResultsAsObjectArray();
        $parsedOptionGroup = array();
        foreach ($results as $result) {
            $parsedOption = $this->_parseDataRetrievedFromDatabase($result);
            array_push($parsedOptionGroup, $parsedOption);
        }
        return $parsedOptionGroup;
    }
    
    
    /**
     * Parse the data retrieved from the DB
     * @param array $data
     * @return \stdClass 
     */
    private function _parseDataRetrievedFromDatabase($dataFromDb) {
        $tmpOption = new stdClass();
        $tmpOption->value = $dataFromDb->{$this->_optionValueField};
        $tmpOption->text = $dataFromDb->{$this->_optionTextField};
        return $tmpOption;
    }
    
    /**
     * @TODO: may be not necessary this method. check Exception
     * Validate the queryParameters set
     * @param array $queryParameters 
     */
    private function _validateQueryParameters(array $queryParameters) {
        if (!is_array($queryParameters)) {
            //throw exception
            die("queryParameters must be an array");
        }
        if (empty($queryParameters)) {
            //throw exception
            die("queryParameters can't be empty");
        }
        if (!is_string($queryParameters["tableName"])) {
            //throw exception
            die("Table name is not a string");
        }
        if (empty($queryParameters["tableName"])) {
            //throw exception
            die("Table name is empty");
        }

        if (!is_array($queryParameters["fields"])) {
            //throw exception
            die("Fields must be an array");
        }

        if (empty($queryParameters["fields"])) {
            //throw exception
            die("Fields array can't be empty");
        }
    }
    
        /**
     * Establece los campos de referencia de la bbdd para rellenar los atributos de las 'option'
     * @example array(
     *              "value" => "el campo de la bbdd que estará relacionado con el atributo 'value'",
     *         		"text" => "el campo de la bbdd que estará relacionado con el texto de la 'option'",
     *          )
     * @param array $references 
     */
    public function setReferenceFields(array $references) {
        $this->_optionValueField = $references["value"];
        $this->_optionTextField = $references["text"];
    }

    /**
     * Set the query data in order to be able to get the options values and text from the db
     * to populate the select element.
     * @example $queryParameters = array("tableName" => "the db table to fetch the data",
     * 					"fields" = array("fieldName" => "fieldAlias") || array("fieldName")
     * 				    )
     * @param array $queryParameters 
     */
    public function setQueryParametersToRetrieveTheOptionData(array $queryParameters) {
        $this->_validateQueryParameters($queryParameters);
        $this->_simpleSelect = new SimpleSelect($queryParameters["tableName"]);
        $this->_simpleSelect->setFields($queryParameters["fields"]);
    }

    public function getOptionsData(array $parameters = array()) {
        
    }
}

?>
